# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=nghttp2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.65.0
pkgrel=1
pkgdesc="Framing layer of HTTP/2 is implemented as a reusable C library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://nghttp2.org/'
msys2_repository_url="https://github.com/nghttp2/nghttp2"
msys2_references=(
  "archlinux: libnghttp2"
  "cygwin: nghttp2"
  "gentoo: net-libs/nghttp2"
  "cpe: cpe:/a:nghttp2:nghttp2"
)
depends=()
checkdepends=("${MINGW_PACKAGE_PREFIX}-cunit")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
)
license=('spdx:MIT')
source=("https://github.com/nghttp2/nghttp2/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz"
        "001-fix-pc-prefix.patch"
        "002-nghttp2-pkgconfig-add-cflags-private.patch")
sha256sums=('f1b9df5f02e9942b31247e3d415483553bc4ac501c87aa39340b6d19c92a9331'
            'baf901e7d565eaea1133d30b13620b02681ce96ebe56d38e1dea4394299106b6'
            'd82d0b5b32e26955c99ef70e129b33696fee01ad9a9a4e5d0fb98951fb5d0f22')

prepare() {
  cd $srcdir/${_realname}-${pkgver}

  patch -Nbp1 -i "${srcdir}/001-fix-pc-prefix.patch"
  patch -Nbp1 -i "${srcdir}/002-nghttp2-pkgconfig-add-cflags-private.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-shared-${MSYSTEM}" && cd "${srcdir}/build-shared-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_STATIC_LIBS=OFF \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DENABLE_LIB_ONLY=ON \
      -DWITH_JEMALLOC=OFF \
      -DWITH_LIBXML2=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-static-${MSYSTEM}" && cd "${srcdir}/build-static-${MSYSTEM}"

  export CPPFLAGS="$CPPFLAGS -DLIBXML_STATIC -DNGHTTP2_STATICLIB"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_STATIC_LIBS=ON \
      -DENABLE_LIB_ONLY=ON \
      -DWITH_JEMALLOC=OFF \
      -DWITH_LIBXML2=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-static-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}/build-shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
